package com.jdls.tanhua.web;

import com.jdls.tanhua.commons.utils.JwtUtils;
import com.jdls.tanhua.exception.AllException;
import com.jdls.tanhua.interceptor.UserHolder;
import com.jdls.tanhua.pojo.Question;
import com.jdls.tanhua.pojo.User;
import com.jdls.tanhua.pojo.UserInfo;
import com.jdls.tanhua.server.UserInfoServer;
import com.jdls.tanhua.vo.*;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwt;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author 嘉德罗斯
 * @date 2021/10/23
 */

@RestController
@RequestMapping("/users")
public class UsersWeb {

    @Autowired
    private UserInfoServer userInfoServer;

    @GetMapping
    //查询用户的资料
    public ResponseEntity find( Long userID,
                               @RequestHeader("Authorization") String token){

        if(userID==null){
           userID = UserHolder.getUserId();
        }
        UserInfo userInfo = userInfoServer.find(userID);
        return ResponseEntity.ok(userInfo);
    }

    @PutMapping
    public ResponseEntity update(@RequestBody UserInfo userInfo,
                                 @RequestHeader("Authorization") String token){

        Claims body = JwtUtils.getBody(token);
        Long id =Long.valueOf((Integer) body.get("id"));
        userInfo.setId(id);
        if (userInfo.getNickname() == ""||userInfo.getNickname() ==null){
            throw new AllException(ErrorResult.nameErr());
        }
        userInfoServer.update(userInfo);
        UserInfo userInfo1 = userInfoServer.find(id);
        return ResponseEntity.ok(userInfo1);

    }

    @PostMapping("/header")
    public ResponseEntity header(@RequestHeader("Authorization")String token,
                                 @RequestBody MultipartFile headPhoto) throws IOException {

        Claims body = JwtUtils.getBody(token);
        Integer id =(Integer) body.get("id");
        userInfoServer.update(id,headPhoto);
        Long userId = Long.valueOf(id);
        UserInfo userInfo = userInfoServer.find(userId);
        return ResponseEntity.ok(userInfo);
    }

    @GetMapping("/settings")
    public ResponseEntity settings(){

        User user = UserHolder.get();

        Long id = user.getId();
        String phone = user.getMobile();
        SettingsVo vo = userInfoServer.settings(id,phone);
        return ResponseEntity.ok(vo);
    }

    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map){
        String content = (String) map.get("content");
        Long userId = UserHolder.getUserId();
        userInfoServer.questions(userId,content);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/notifications/setting")
    public ResponseEntity setting(@RequestBody Map map){
        Long userId = UserHolder.getUserId();
        userInfoServer.setting(map,userId);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") int page,
                                    @RequestParam(defaultValue = "10") int pageSize){

        PageResult pr = userInfoServer.blacklist(page,pageSize);
        return ResponseEntity.ok(pr);
    }


    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity blacklist(@PathVariable Long uid){
        userInfoServer.delete(uid);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendCode(){
        userInfoServer.sendCode();
        return ResponseEntity.ok(null);
    }

    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity code(@RequestBody Map verificationCode){
        String code = (String) verificationCode.get("verificationCode");
        Map map = userInfoServer.code(code);
        return ResponseEntity.ok(map);
    }

    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody Map map){

        String phone = (String) map.get("phone");
        userInfoServer.update(phone);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/counts")
    //查看喜欢,互相喜欢,粉丝数量
    public ResponseEntity counts(){
        CountsVo countsVo = userInfoServer.counts();

        return ResponseEntity.ok(countsVo);
    }

    @GetMapping("/friends/{type}")
    //互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
    public ResponseEntity friends(@PathVariable Integer type,
                                  @RequestParam(defaultValue = "1") int page,
                                  @RequestParam(defaultValue = "10") int pageSize,
                                  String nickname){
        System.out.println(type);
        PageResult pageResult = userInfoServer.friends(type,page,pageSize,nickname);
        return ResponseEntity.ok(pageResult);
    }
}
